/*
 * @Author: shiguo
 * @Date: 2022-05-05 14:19:39
 * @LastEditors: shiguo
 * @LastEditTime: 2023-03-23 15:15:06
 * @FilePath: /@aks-dev/easyui/jsbridge/UpgradeModule.ts
 */
import { NativeModules, PermissionsAndroid, Platform } from 'react-native';

const { UpgradeModule } = NativeModules;

import type { UpgradeOptions } from './index.d'

export const getAppVersion = () => UpgradeModule.getAppVersion();

export const getOSVersion = () => UpgradeModule.getOSVersion();

export const upgrade = async (options: UpgradeOptions ) => {
    const { title = '提示' } = options

    if (Platform.OS == 'android') {
        try {
            const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, {
                title: '提示',
                message: "应用升级需要获取APP的写入权限",
                buttonNeutral: "稍后再询问",
                buttonNegative: "取消",
                buttonPositive: "确定"
            })
            if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                return UpgradeModule.upgrade(options)
            }

            return Promise.reject("没有应用升级权限！")
        } catch (err) {
            return Promise.reject("请求权限异常！")
        }

    }

    return UpgradeModule.upgrade(options)
}